package com.yl.chainStore.control.company

import com.yl.chainStore.entity.system.CompanyDictionaryDetail
import com.yl.chainStore.pojo.vo.company.CompanyDictionaryDetailUpdateVo
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RequestMapping("company/dept/config")
@RestController
open class SubCompanyConfigController(
    private val iCompanyDictionaryService: ICompanyDictionaryService
) {

    @RequestMapping("getList/{id}")
    open fun getList(@PathVariable id : Long): ResponseBody {
        val companyDetailList =
            iCompanyDictionaryService.getCompanyDetailList(id, getContext().companyId, getContext().subCompanyId)
        return returnSuccess(companyDetailList)
    }

    @RequestMapping("addDetail")
    @Transactional(rollbackFor = [Exception :: class])
    open fun addDetail(@RequestBody detail: CompanyDictionaryDetail): ResponseBody {
        val updateDetail =
            iCompanyDictionaryService.addDetail(detail)
        if(updateDetail.bodyIsError()){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return  updateDetail
        }
        return returnSuccess()
    }



    @RequestMapping("getDetailById/{id}")
    open fun getDetailById(@PathVariable id : Long): ResponseBody {
        val detail =
            iCompanyDictionaryService.getDetail(id)
        return returnSuccess(detail)
    }

    @RequestMapping("updateDetail")
    @Transactional(rollbackFor = [Exception :: class])
    open fun updateDetail(@RequestBody vo: CompanyDictionaryDetailUpdateVo): ResponseBody {
        val updateDetail =
            iCompanyDictionaryService.updateDetail(vo)
        if(updateDetail.bodyIsError()){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return  updateDetail
        }
        return returnSuccess()
    }


    @RequestMapping("delDetail/{id}")
    @Transactional(rollbackFor = [Exception :: class])
    open fun delDetail(@PathVariable id: Long): ResponseBody {
        val delDetailByDetailId =
            iCompanyDictionaryService.delDetailByDetailId(id)
        if(delDetailByDetailId.bodyIsError()){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
            return  delDetailByDetailId
        }
        return returnSuccess()
    }

}